﻿using System;
using System.Collections; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Data;
using System.Text;
using System.Xml;
using System.IO;
using Comm.PublicClass;

namespace WsProtocol.PublicClass
{
    public class PtlCommunal
    {
                  
        /// <summary>
        /// 获取特殊处理信息
        /// </summary>
        /// <param name="funcstr"></param>
        /// <returns></returns>
        public static PtlPubConsts.SValueFunc GetFuncInfo(string funcstr)
        {
            PtlPubConsts.SValueFunc result = new PtlPubConsts.SValueFunc();
            string valuefunc = funcstr.Replace(PtlPubConsts.datahead, "").Replace(PtlPubConsts.dataend, "");
            string[] datalst = valuefunc.Split(';');
            result.FuncName = datalst[0].ToUpper();  //功能名称 即要做什么

            result.NodeName = datalst[1];  //针对的节点名
            return result;
        }

        

        /// <summary>
        /// 增加小数点

        /// </summary>
        /// <param name="yourstr"></param>
        /// <returns></returns>
        public static string AddPoint(string yourstr,int point)
        {
            if (point == 0)
                return yourstr;
            double tmp;
            if (yourstr.IndexOf('.') >= 0)
                tmp = Convert.ToDouble(yourstr);
            else
                tmp = Convert.ToInt32(yourstr) * Math.Pow(10, -point);
            return Convert.ToString(tmp);
        }
        
        

        ////
        ///// <summary>
        ///// 增加计数  只增加里面的其中一个累加值
        ///// </summary>
        ///// <param name="pstr">原始字符串</param>
        ///// <param name="ino">指定计数 -1表示无效</param>
        ///// <param name="appendno">在当前计数基础上的增加值</param>
        ///// <param name="front">true表示从前面找   false表示从后面找</param>
        ///// <returns></returns>
        //public static string AddNOOnce(string pstr, int ino, int appendno, bool front)
        //{
        //    string resultstr = pstr;
        //    if (front)
        //    {
        //        int Fpos = pstr.IndexOf('[');
        //        int Lpos = pstr.IndexOf(']');
        //        string otherstr = string.Empty;
        //        if (Fpos >= 0 && Lpos >= 0)
        //        {
        //            if (Fpos > 0)
        //                otherstr = pstr.Substring(0, Fpos);

        //            int No = ino;
        //            if (No < 0)
        //            {
        //                string nostr = pstr.Substring(Fpos + 1, Lpos - Fpos - 1);
        //                No = Convert.ToInt32(nostr);
        //                No += appendno;
        //            }

        //            resultstr = '['
        //                      + No.ToString()
        //                      + ']'
        //                      + pstr.Substring(Lpos + 1);
        //        }
        //        //if (otherstr.IndexOf('[') >= 0 && otherstr.IndexOf(']') >= 0)
        //        //    otherstr = AddNO(otherstr);
        //        resultstr = otherstr + resultstr;
        //    }
        //    else
        //    {
        //        int Fpos = pstr.LastIndexOf('[');
        //        int Lpos = pstr.LastIndexOf(']');
        //        string otherstr = string.Empty;
        //        if (Fpos >= 0 && Lpos >= 0)
        //        {
        //            if (Fpos > 0)
        //                otherstr = pstr.Substring(0, Fpos);
        //            int No = ino;
        //            if (No < 0)
        //            {
        //                string nostr = pstr.Substring(Fpos + 1, Lpos - Fpos - 1);
        //                No = Convert.ToInt32(nostr);
        //                No += appendno;
        //            }
        //            resultstr = '['
        //                      + No.ToString()
        //                      + ']'
        //                      + pstr.Substring(Lpos + 1);
        //        }
        //        //if (otherstr.LastIndexOf('[') >= 0 && otherstr.LastIndexOf(']') >= 0)
        //        //    otherstr = AddNO(otherstr);
        //        resultstr = otherstr + resultstr;
        //    }
        //    return resultstr;
        //}
        

        ////增加计数
        //public static string AddNO(string pstr)
        //{
        //    string resultstr = pstr;
        //    int Fpos = pstr.LastIndexOf('[');
        //    int Lpos = pstr.LastIndexOf(']');
        //    string otherstr = string.Empty;
        //    if (Fpos >= 0 && Lpos >= 0)
        //    {
        //        if (Fpos > 0)
        //            otherstr = pstr.Substring(0, Fpos);

        //        string nostr = pstr.Substring(Fpos + 1, Lpos - Fpos - 1);
        //        int No = Convert.ToInt32(nostr);
        //        No += 1;
        //        resultstr = '['
        //                  + No.ToString()
        //                  + ']'
        //                  + pstr.Substring(Lpos + 1);
        //    }
        //    if (otherstr.LastIndexOf('[') >= 0 && otherstr.LastIndexOf(']') >= 0)
        //        otherstr = AddNO(otherstr);
        //    resultstr = otherstr + resultstr;
        //    return resultstr;
        //}


        ////增加计数  指定数值
        //public static string AddNO(string pstr, int ino)
        //{
        //    string resultstr = pstr;
        //    int Fpos = pstr.LastIndexOf('[');
        //    int Lpos = pstr.LastIndexOf(']');
        //    string otherstr = string.Empty;
        //    if (Fpos >= 0 && Lpos >= 0)
        //    {
        //        if (Fpos > 0)
        //            otherstr = pstr.Substring(0, Fpos);

        //        resultstr = '['
        //                  + ino.ToString()
        //                  + ']'
        //                  + pstr.Substring(Lpos + 1);
        //    }
        //    resultstr = otherstr + resultstr;
        //    return resultstr;
        //}
        


        private static void CopySonNode(XmlDocument xmldoc, XmlNode sonnode, XmlNode tnode)
        {
            XmlNode newson = sonnode.Clone();
            //sonnode.Attributes["repeat"] = "0";  
            //sonnode.SetAttribute("repeat", "0");   
           
            
            if (sonnode.HasChildNodes && sonnode.ChildNodes[0].Name != "#text")
            {
                for (int i = 0;i < sonnode.ChildNodes.Count;i++)
                    CopySonNode(xmldoc, sonnode.ChildNodes[i], newson);
            }
            tnode.AppendChild(newson);
        }

        ///// <summary>
        ///// 复制XML节点
        ///// </summary>
        ///// <param name="sourcenode"></param>
        ///// <param name="targetnode"></param>
        //public static int CopyXMLNode(XmlDocument xmldoc, XmlNode sourcenode, XmlNode targetnode)
        //{
        //    int copynodecount = 0;  //复制的节点个数
        //    int childcount = sourcenode.ChildNodes.Count;
        //    for (int i = 0; i < childcount; i++)
        //    {
        //        XmlElement xe = (XmlElement)sourcenode.ChildNodes[i];
        //        if (xe.HasAttribute("repeat") && xe.GetAttribute("repeat") != "y")
        //            continue;
        //        copynodecount++;
        //        XmlNode newson = xe.CloneNode(true);
        //        XmlNode namenode = newson.SelectSingleNode("itemname");
        //        if (namenode != null)
        //            namenode.InnerText = AddNOOnce(namenode.InnerText, -1, 1, true);

        //        newson.SelectSingleNode("itemdata").InnerText = string.Empty;
        //        newson.SelectSingleNode("itemvalue").InnerText = string.Empty;

        //        //if (newson.Name == "itemname")
        //        //    newson.Value = AddNO(newson.Value);
        //        targetnode.AppendChild(newson);

        //        if (xe.HasAttribute("repeat") && xe.GetAttribute("repeat") == "y")
        //            xe.SetAttribute("repeat", "n");  //将原来的可重复节点置为不可重复，重复节点由复制出的新节点代替
        //    }
        //    return copynodecount;
        //}

        

        /// <summary>
        /// 将数据按****.**的格式分解
        /// </summary>
        /// <param name="datastr">原始数据</param>
        /// <param name="format">数据格式</param>
        /// <returns></returns>
        public static string Explaindata1(string datastr,string format)
        {
            if (string.IsNullOrEmpty(datastr))
                return string.Empty;
            if (string.IsNullOrEmpty(format)) 
                return datastr;
            datastr = datastr.Replace("\r", "").Replace("\n", "");
            format = format.Replace("\r", "").Replace("\n", "");
            int p = -1;
            StringBuilder formatstr = new StringBuilder(format);

            for (int i = 0; i < formatstr.Length; i++)
            {
                if (formatstr[i] == '*')
                {
                    p++;
                    if (datastr.Length <= p) 
                        break;
                    formatstr.Replace("*", datastr[p].ToString(), i, 1);
                }
            
            }
            return formatstr.ToString();
        }

        /// <summary>
        /// 将数据按**:**?**:**的格式解析
        /// </summary>
        /// <param name="datastr">原始数据</param>
        /// <param name="format">数据格式</param>
        /// <returns></returns>
        public static string Explaindata2(string datastr,string format)
        {
            if (string.IsNullOrEmpty(datastr))
                return string.Empty;
            if (string.IsNullOrEmpty(format) || format == "?")
                return datastr;

            datastr = datastr.Replace("\r", "").Replace("\n", "");
            format = format.Replace("\r", "").Replace("\n", "");

            int p = format.IndexOf("?");
            if (p < 0 ) 
                return datastr;

            string value = datastr;
            string tmpstr;
            char splitchar = ':';
            tmpstr = format.Substring(0,p);
            string[] parlist = tmpstr.Split(splitchar);
            tmpstr = format.Substring(p+1,format.Length - p - 1);
            string[] valuelist = tmpstr.Split(splitchar);

            if (parlist.Length != valuelist.Length) 
                return value;
            
            for(int i = 0;i < parlist.Length;i++)
            {
                if (parlist[i] == datastr)
                {
                    value = valuelist[i];
                    if (datastr != valuelist[i] && valuelist.Length > 2)
                        value = "(" + datastr.PadLeft(2, '0') + ")" + valuelist[i];
                    
                        
                    break;
                }
            }
            return value;
        }

        /// <summary>
        /// 将数据按****.**的格式合成
        /// </summary>
        /// <param name="datastr"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Createdata1(string datastr, string format) 
        {
            try
            {
                //shaunwang 2019-1-23修改
                //20**-**-**- 
                //20 1- 1-1 

                //string adata = datastr;
                //List<string> splitlst = new List<string>();

                //for (int i = 0; i < format.Length; i++)
                //    if (format[i] != '*')
                
                //先获取所有分隔符
                string aformat = format.Replace("*", "~@~");
                string[] splitlst = aformat.Split(new string[] {"~@~"}, StringSplitOptions.RemoveEmptyEntries); 
                
                string[] datalst = datastr.Split(splitlst, StringSplitOptions.RemoveEmptyEntries);
                string[] formatlst = format.Split(splitlst, StringSplitOptions.RemoveEmptyEntries);

                if (datalst.Length != formatlst.Length)  //格式不匹配  可能直接传入的就是值
                    return datastr;
                
                StringBuilder newstr = new StringBuilder("");
                for (int i = 0; i < formatlst.Length; i++)
                {
                    if (datalst.Length > i)
                    {
                        newstr.Append(datalst[i].PadLeft(formatlst[i].Length, '0'));
                    }
                    else
                        newstr.Append("".PadLeft(formatlst[i].Length, '0'));
                }
                
                
                //for (int i = 0; i < format.Length; i++)
                //{
                //    if (format[i] == '*')
                //        newstr.Append(datastr[i]);
                //}
                return newstr.ToString();
            }
            catch (Exception ex) {
                return datastr;
            }

        }

        /// <summary>
        /// 将数据按**:**?**:**的格式合成
        /// </summary>
        /// <param name="datastr"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Createdata2(string datastr, string format)
        {
            if (format == null || format == string.Empty || format =="?") return datastr;

            int p = format.IndexOf("?");
            if (p < 0 ) return datastr;

            string value = string.Empty;
            string tmpstr;
            char splitchar = ':';
            tmpstr = format.Substring(0,p);
            string[] parlist = tmpstr.Split(splitchar);
            tmpstr = format.Substring(p + 1, format.Length - (p + 1));
            string[] valuelist = tmpstr.Split(splitchar);

            if (parlist.Length != valuelist.Length) return value;
            
            for(int i = 0;i < valuelist.Length;i++)
            {
                if (valuelist[i] == datastr || "(" + MyConvert.DecToHex(parlist[i], 2) + ")" + valuelist[i] == datastr
                    || "(" + parlist[i].PadLeft(2, '0') + ")" + valuelist[i] == datastr
                    || "(" + parlist[i] + ")" + valuelist[i] == datastr)
                {
                    value = parlist[i];
                    break;
                }
            }
            return value;
        
        }

        /// <summary>
        /// 按照固定格式替换值 by xd 2017-06-02
        /// </summary>
        /// <param name="datastr"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string ReplaceByFormat(string datastr, string format,string replacestr)
        {
            if (format == null || format == string.Empty || replacestr=="") return datastr;
            int p = -1;
            StringBuilder  strbtemp=new StringBuilder();
            StringBuilder formatstr = new StringBuilder();
            for (int i = 0; i < replacestr.Length; i++)
            {
                strbtemp.Append("*");
            }
            while (format.Contains(replacestr))
            {
                p = format.IndexOf(replacestr);
                datastr=datastr.Substring(0, p) + replacestr + datastr.Substring(p + replacestr.Length);
                format = format.Substring(0, p) + strbtemp.ToString() + format.Substring(p + replacestr.Length);
            }

            return datastr;

        }

        /// <summary>
        /// 将帧内的数据域按指定数据格式解析
        /// </summary>
        /// <param name="yourdata">数据域</param>
        /// <param name="datatype">数据类型</param>
        /// <param name="display">分解描述</param>
        /// <returns></returns>
        public static string FrameToData(string yourdata, string format, PtlPubConsts.TDataType datatype, string display)
        {
            string resultstr = yourdata;
            switch(datatype)
            {
                case PtlPubConsts.TDataType.DTBCD:  //BCD
                    bool allF = true;
                    for (int i = 0; i < yourdata.Length; i++)
                    {
                        if (yourdata[i] != 'F')
                        {
                            allF = false;
                            break;                        
                        }                    
                    }
                    if (allF)
                        display = string.Empty;
                    //处理符号位
                    if (display == "0:1?+:-" && yourdata.Length >= 4)
                    {
                        string signstr = yourdata.Substring(0, 2);
                        signstr = Convert.ToString(Convert.ToInt32(signstr, 16), 2);  //转为2进制
                        signstr = signstr.PadLeft(8, '0');
                        resultstr = Explaindata2(signstr.Substring(0, 1), display);  //符号

                        signstr = Convert.ToString(Convert.ToInt32(signstr.Substring(1), 2), 16);  //转为16进制
                        yourdata = signstr.PadLeft(2, '0')
                                 + yourdata.Substring(2, yourdata.Length - 2);
                        resultstr = resultstr
                                  + Explaindata1(yourdata, format);
                    }
                    else
                    {
                        resultstr = Explaindata1(yourdata, format);

                    }
                    break;
                case PtlPubConsts.TDataType.DTASCII:  //ASCII码

                    //StringBuilder str = new StringBuilder("");
                    //for (int i = 0; i < yourdata.Length; i += 2)
                    //{
                    //    string data = yourdata.Substring(i, 2);
                    //    int idata = Convert.ToInt32(data, 16);
                    //    str.Append(Chr(idata));
                    //}
                    //resultstr = str.ToString();

                    resultstr = MyConvert.HexToStr(yourdata);

                    break;
                case PtlPubConsts.TDataType.DTHex:  //十六进制
                    int tmpint = Convert.ToInt32(yourdata, 16);
                    resultstr = tmpint.ToString();
                    break;
                case PtlPubConsts.TDataType.DTFloat:  //单精度浮点数
                    byte[] b = new byte[4];
                    yourdata = ProtocolRelated.ChangeAddress(yourdata);
                    if (yourdata.Length != 8)
                        resultstr = string.Empty;
                    else
                    {
                        for (int i = 0; i < yourdata.Length; i += 2)
                            b[i / 2] = Convert.ToByte(yourdata.Substring(i, 2), 16);
                        float t = BitConverter.ToSingle(b, 0);
                        resultstr = Convert.ToString(t);
                    }
                    break;
                case PtlPubConsts.TDataType.DTDouble:  //双精度浮点数
                    byte[] d = new byte[8];
                    yourdata = ProtocolRelated.ChangeAddress(yourdata);
                    if (yourdata.Length != 16)
                        resultstr = string.Empty;
                    else
                    {
                        for (int i = 0; i < yourdata.Length; i += 2)
                            d[i / 2] = Convert.ToByte(yourdata.Substring(i, 2), 16);
                        double t = BitConverter.ToDouble(d, 0);
                        resultstr = Convert.ToString(t);
                    }
                    break;
                case PtlPubConsts.TDataType.DTInt:   //十进制

                     tmpint = Convert.ToInt32(yourdata, 16);
                    resultstr = tmpint.ToString();
                    break;
                case PtlPubConsts.TDataType.DTBin:   //二进制

                    resultstr = Convert.ToString(Convert.ToInt32(yourdata, 16),2);
                    break;            
            
            }
            return resultstr;
        }

        /// <summary>
        /// 将二进制数据转为指定格式
        /// </summary>
        /// <returns></returns>
        public static string BitToData(string yourdata, PtlPubConsts.TDataType datatype)
        {
            string resultstr = yourdata;
            switch (datatype)
            {
                case PtlPubConsts.TDataType.DTInt:   //十进制

                    int tmpint = Convert.ToInt32(yourdata, 2);
                    resultstr = tmpint.ToString();
                    break;
                case PtlPubConsts.TDataType.DTBin:   //二进制

                    resultstr = yourdata;
                    break; 
            }
            return resultstr;
        }

        /// <summary>
        /// 把数据格式中其他格式统一为*的格式
        /// </summary>
        /// <param name="formatstr"></param>
        /// <returns></returns>
        public static string InitFormat(string formatstr)
        {
            formatstr = formatstr.ToUpper();
            StringBuilder format = new StringBuilder(formatstr);
            format.Replace('Y','*');
            format.Replace('M','*');
            format.Replace('D','*');
            format.Replace('W','*');
            format.Replace('H', '*');
            format.Replace('h','*');
            format.Replace('m','*');
            format.Replace('S', '*');
            format.Replace('F','*');
            format.Replace('X','*');           
            format.Replace('N','*');
            format.Replace('L', '*');
            format.Replace('T', '*');
            return format.ToString();
        }

        

        /// <summary>
        /// 将传入的参数转为帧内数据(字节)
        /// </summary>
        /// <param name="avalue">传入的参数</param>
        /// <param name="aformat">参数格式</param>
        /// <param name="datatype">数据类型</param>
        /// <returns></returns>
        public static string ValueToByteData(string avalue, string aformat, PtlPubConsts.TDataType datatype)
        {
            string resultstr = string.Empty;
            int formatnum = ProtocolRelated.GetStrNum(aformat, '*');
            if (avalue == null || avalue == string.Empty) return avalue.PadLeft(formatnum,'0');
            int charord;
            int findex ;
            switch(datatype)            
            {
                case PtlPubConsts.TDataType.DTBCD:  //BCD
                    findex= aformat.LastIndexOf('.');
                    if (findex > 0)   //对于带小数点的格式处理
                    {
                        int vindex = avalue.LastIndexOf('.');
                        if (vindex < 0)  //输入参数未带小数点  NNN.N  500
                        {
                            if (avalue.Length <= findex)
                            {
                                avalue = avalue + ".";
                                for (int i = 1; i < (aformat.Length - findex); i++)
                                    avalue = avalue + "0";

                            }
                            else
                            {
                                avalue = avalue.Insert(findex, ".");
                                avalue = avalue.Substring(0, aformat.Length);
                            
                            }
                        }
                        else           //输入参数带了小数点
                        {
                            string[] valueinfo = avalue.Split('.');
                            string value1 = valueinfo[0].PadLeft(findex, '0'); //InitStr(valueinfo[0], findex, "0", true);
                            string value2 = valueinfo[1].PadRight(aformat.Length - findex - 1, '0');  //InitStr(valueinfo[1], aformat.Length - findex - 1, "0", false);
                            avalue = value1 + "." + value2;
                            avalue = avalue.Substring(0, aformat.Length);
                            
                        }        
                        
                    }

                    avalue = avalue.PadLeft(aformat.Length, '0');
                    resultstr = Createdata1(avalue,aformat);
                    break;
                case PtlPubConsts.TDataType.DTASCII:  //ASCII码

                    //for (int i = 0;i < avalue.Length;i++)
                    //{
                    //    charord = Ord(avalue[i].ToString());
                    //    resultstr += Convert.ToString(charord,16).PadLeft(2,'0');
                    //}
                    //resultstr = resultstr.PadRight(formatnum,'0'); 

                    resultstr = MyConvert.StrToHex(avalue).PadRight(formatnum, '0'); 
                    break;
                case PtlPubConsts.TDataType.DTInt:   //十进制

                    resultstr = Convert.ToString(Convert.ToInt32(avalue),16).PadLeft(formatnum,'0');
                    break;
                case PtlPubConsts.TDataType.DTHex:   //十六进制　
                　　//hxh 2015-12-16 加入对十六进制数格式中有小数点的处理
                    findex= aformat.LastIndexOf('.');
                    if (findex > 0)   //对于带小数点的格式处理
                    {
                        int vindex = avalue.LastIndexOf('.');
                        if (vindex < 0)  //输入参数未带小数点  NNN.N  500
                        {
                            if (avalue.Length <= findex)
                            {
                                
                                for (int i = 1; i < (aformat.Length - findex); i++)
                                    avalue = avalue + "0";

                            }
                            else
                            {
                                avalue = avalue.Insert(findex, ".");
                                avalue = avalue.Substring(0, aformat.Length);

                            }
                        }
                        else           //输入参数带了小数点
                        {
                            string[] valueinfo = avalue.Split('.');
                            string value1 = valueinfo[0].PadLeft(findex, '0'); //InitStr(valueinfo[0], findex, "0", true);
                            string value2 = valueinfo[1].PadRight(aformat.Length - findex - 1, '0');  //InitStr(valueinfo[1], aformat.Length - findex - 1, "0", false);
                            avalue = value1 + value2;                           

                        }
                    }
                    resultstr = Convert.ToString(Convert.ToInt32(avalue), 16).PadLeft(formatnum, '0');// avalue.PadLeft(formatnum, '0'); 
                    break;
                case PtlPubConsts.TDataType.DTBin:   //二进制

                    resultstr = Convert.ToString(Convert.ToInt32(avalue,2),16).PadLeft(formatnum,'0');
                    break;
                case PtlPubConsts.TDataType.DTFloat:  //浮点数
                    byte[] d = BitConverter.GetBytes(Convert.ToDouble(avalue));
                    break;
            
            }
            return resultstr;
        
        }

        /// <summary>
        /// 将传入的参数转为帧内数据(字节)
        /// </summary>
        /// <param name="avalue">传入的参数</param>
        /// <param name="aformat">参数格式</param>
        /// <param name="datatype">数据类型</param>
        /// <returns></returns>
        public static string ValueToBitData(string avalue, string aformat, PtlPubConsts.TDataType datatype)
        {
            string resultstr = string.Empty;
            int formatnum = ProtocolRelated.GetStrNum(aformat, '*');
            if (avalue == null || avalue == string.Empty) return avalue.PadLeft(formatnum,'0');
            switch(datatype)            
            {
                case PtlPubConsts.TDataType.DTInt:   //十进制

                    resultstr = Convert.ToString(Convert.ToInt32(avalue),2);
                    resultstr = avalue.PadLeft(formatnum,'0');
                    break;
                case PtlPubConsts.TDataType.DTBin:   //二进制

                    resultstr = avalue.PadLeft(formatnum,'0');
                    break;

            }
            return resultstr;
        }

        /// <summary>
        /// 复制类属性 3参数
        /// </summary>
        /// <param name="fromFields"></param>
        /// <param name="fromRecord"></param>
        /// <param name="toRecord"></param>
        public static void SetProperties(PropertyInfo[] fromFields,
                                         object fromRecord,
                                         object toRecord)
        {
            PropertyInfo fromField = null;

            try
            {

                if (fromFields == null)
                {
                    return;
                }

                for (int f = 0; f < fromFields.Length; f++)
                {

                    fromField = (PropertyInfo)fromFields[f];

                    fromField.SetValue(toRecord,
                                       fromField.GetValue(fromRecord, null),
                                       null);
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 将参数值字符串拆分成有上下级关系的节点
        /// </summary>
        /// <param name="avalue"></param>
        /// <returns></returns>
        public static ProNodeBase SplitValueToNode(string avalue)
        {
            ProNodeBase anode = new ProNodeBase();  //创建一个对象
            if (string.IsNullOrEmpty(avalue))
                return anode;
            //if (avalue.StartsWith("{") && avalue.EndsWith("}"))  //去掉最外层括号
            //    avalue = avalue.Substring(1, avalue.Length - 2);
            
            int ilevel = 0;
            bool haveend = false;
            ValueToSonNode(anode, ref avalue, ref ilevel, haveend);
            return anode;
        }

        /// <summary>
        /// 参数值字符串拆分至子节点
        /// </summary>
        /// <param name="pnode"></param>
        /// <param name="avalue"></param>
        private static void ValueToSonNode(ProNodeBase pnode, ref string avalue, ref int ilevel, bool haveend)
        {
            if (pnode == null || string.IsNullOrEmpty(avalue))
                return;
            string tmpvalue = string.Empty;
            for (int i = 0; i < avalue.Length; i++)
            {
                tmpvalue += avalue[i];  //记录参数值
                if (avalue[i] == '{')  //开始下一级  需要新增下级节点
                {
                    ilevel++;
                    tmpvalue = string.Empty;
                    pnode = pnode.AddSonNode();
                    if (avalue.Length == 1)
                        return;
                    avalue = avalue.Substring(i + 1);
                    ValueToSonNode(pnode, ref avalue, ref ilevel, false);
                }
                else
                    if (avalue[i] == ',')
                    {
                        pnode = pnode.AddSonNode();
                        pnode.inputValue = tmpvalue.Substring(0, tmpvalue.Length - 1);  //去掉最后一个 ,或 }
                        pnode = pnode.ParentNode;
                        
                        avalue = avalue.Substring(i + 1);
                        tmpvalue = string.Empty;

                        ValueToSonNode(pnode, ref avalue, ref ilevel, false);
                    }
                    else
                        if (avalue[i] == '}') //本级搞完  需要返回上一级
                        {
                            if (!haveend)
                            {
                                pnode = pnode.AddSonNode();
                                pnode.inputValue = tmpvalue.Substring(0, tmpvalue.Length - 1);  //去掉最后一个 ,或 }
                                pnode = pnode.ParentNode;
                            }

                            bool ifend = true;
                            pnode = pnode.ParentNode;
                            if (pnode == null)
                                return;
                            ilevel--;
                            avalue = avalue.Substring(i + 1);
                            if (avalue.StartsWith(","))  //本级之后还有的话就把逗号去掉
                            {
                                avalue = avalue.Substring(1);
                                ifend = false;
                            }
                            tmpvalue = string.Empty;

                            ValueToSonNode(pnode, ref avalue, ref ilevel, ifend);
                        }




                    //if (avalue[i] == ',' || avalue[i] == '}')
                    //{
                    //    //if (!(tmpvalue == "}" && avalue.StartsWith("}")))
                    //    //{
                    //        pnode = pnode.AddSonNode();
                    //        pnode.inputValue = tmpvalue.Substring(0, tmpvalue.Length - 1);  //去掉最后一个 ,或 }
                    //        pnode = pnode.ParentNode;
                    //    //}
                    //    if (avalue[i] == ',')  
                    //    {
                    //        avalue = avalue.Substring(i + 1);
                    //    }
                    //    else
                    //        if (avalue[i] == '}')//本级搞完  需要返回上一级
                    //        {
                    //            pnode = pnode.ParentNode;
                    //            if (pnode == null)
                    //                return;
                    //            ilevel--;
                    //            avalue = avalue.Substring(i + 1);

                    //            if (avalue.StartsWith(","))  //本级之后还有的话就把逗号去掉
                    //                avalue = avalue.Substring(1);

                    //        }
                    //    //if (avalue.Length == 1)
                    //    //    return;

                    //    ValueToSonNode(pnode, ref avalue, ref ilevel);
                    //}
            }
        }


        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="source">待加密的明文</param>
        /// <returns></returns>
        public static string Base64Encode(string source)
        {
            string encode = string.Empty;
            byte[] bytes = MyConvert.HexStringToByteArray(source);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }


        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string Base64Decode(string result)
        {
            string decode = string.Empty;
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = MyConvert.ByteArrayToHexString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }

        
    }
}